#include <bits/stdc++.h>
#include <vector>
using namespace std;

class Solution {

    int HSL(vector<int>& nums, int target){
        if(nums.empty()) return -1;
        int left = 0; int right = nums.size() - 1;
        int mid = (left + right) >> 1;
        while(left <= right){   //<=:确保mid的值正确。<时，有时循环结束，mid未更新导致错误
            mid = (left + right) >> 1;
            if(nums[mid] == target){
                if(mid <= 0) break;
                else if(nums[mid-1] != target) break;
                else right = --mid;
            }
            if(nums[mid] < target) left = mid + 1;
            if(nums[mid] > target) right = mid - 1;
        }
        if(nums[mid] == target) return mid;
        else return -1;
    }

    int HSR(vector<int>& nums, int target){
        if(nums.empty()) return -1;
        int left = 0; int right = nums.size() - 1;
        int mid = (left + right) >> 1;
        while(left <= right){
            mid = (left + right) >> 1;
            if(nums[mid] == target){
                if(mid+1 > nums.size()-1) break;
                else if(nums[mid+1] != target) break;
                else left = ++mid;
            }
            if(nums[mid] < target) left = mid + 1;
            if(nums[mid] > target) right = mid - 1;
        }
        if(nums[mid] == target) return mid;
        else return -1;
    }
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> ans;
        ans.push_back(HSL(nums, target));
        ans.push_back(HSR(nums, target));
        return ans;
    }
};

int main(){
    vector<int> nums = {1, 2};
    int target = 2;
    Solution solution;
    auto res = solution.searchRange(nums, target);
    return 0;
}